#include "TableDisplay.h"
#include <QPainter>
#include <QMouseEvent>
static const int IMAGE_WIDTH = 160 * 4;
static const int IMAGE_HEIGHT = 120 * 4;
static const QSize IMAGE_SIZE = QSize(IMAGE_WIDTH, IMAGE_HEIGHT);

void TableDisplay::setImage(QImage &image, int row, int col)
{
    if(row >= 0 && row < m_rowCount && col >= 0 && col < m_colCount)
    {
        QPixmap p = QPixmap::fromImage(image);
        m_pixmap[row][col] = p.scaled(IMAGE_WIDTH, IMAGE_HEIGHT, Qt::IgnoreAspectRatio, Qt::FastTransformation);
        update();
    }
}

void TableDisplay::loadSettings(QSettings &settings)
{
    QRect rect = settings.value("Windows/TableDisplay", QRect(20, 20, 640, 480)).toRect();
    m_colCount = settings.value("NozzleHolder/colCount", 8).toInt();
    m_rowCount = settings.value("NozzleHolder/rowCount", 4).toInt();
    setGeometry(rect);
}

void TableDisplay::writeSettings(QSettings &settings)
{
    QRect rect = geometry();
    settings.setValue("Windows/TableDisplay", rect);
}

void TableDisplay::cleanAll()
{
    for(int row = 0; row < m_rowCount; row ++)
    {
        for(int col = 0; col < m_colCount; col ++)
        {
            m_pixmap[row][col].fill(QColor(128, 128, 128));
        }
    }
}

TableDisplay::TableDisplay(QWidget *parent) : QWidget(parent)
{
    m_colCount = 8;
    m_rowCount = 8;
    for(int row = 0; row < m_rowCount; row ++)
    {
        for(int col = 0; col < m_colCount; col ++)
        {
            //m_image[row][col] = QImage(IMAGE_SIZE, QImage::Format_RGB32);
            m_pixmap[row][col] = QPixmap(IMAGE_SIZE);
            m_pixmap[row][col].fill(QColor(128, 128, 128));
        }
    }

    Qt::WindowFlags	flag = windowFlags();
    setWindowFlags(flag & ~Qt::WindowCloseButtonHint);
}

void TableDisplay::mouseDoubleClickEvent(QMouseEvent * event)
{
    int col = m_colCount * event->x() / width();
    int row = m_rowCount * event->y() / height();
    emit posDBClicked(row, col);

}

void TableDisplay::paintEvent(QPaintEvent * event)
{
    Q_UNUSED(event);
    int w = width();
    int h = height();
    QPainter painter(this);
    painter.setBackground(Qt::white);
    painter.eraseRect(rect());

    double r1 = (double)w / m_colCount / IMAGE_WIDTH;
    double r2 = (double)h / m_rowCount / IMAGE_HEIGHT;
    double r = qMin(r1, r2);
    int offset_x = (w / m_colCount - r * IMAGE_WIDTH) / 2;
    int offset_y = (h / m_rowCount - r * IMAGE_HEIGHT) / 2;

    for(int row = 0; row < m_rowCount; row ++)
    {
        for(int col = 0; col < m_colCount; col ++)
        {
            painter.save();
            painter.translate(col * w  / m_colCount, row * h / m_rowCount);
            painter.translate(offset_x, offset_y);
            painter.scale(r, r);
            painter.drawPixmap(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, m_pixmap[row][col]);
            painter.restore();
        }
    }
    for(int row = 0; row <= m_rowCount; row ++)
    {
        QPoint start(0, row * h / m_rowCount);
        QPoint end(w, row * h / m_rowCount);
        painter.drawLine(start, end);
    }
    for(int col = 0; col <= m_colCount; col ++)
    {
        QPoint start(col * w / m_colCount, 0);
        QPoint end(col * w / m_colCount, h);
        painter.drawLine(start, end);
    }
}

TableDisplay::~TableDisplay()
{

}

